import qs from 'qs'

class Order {
  constructor(http) {
    this.http = http;
  }

  getStatusName(status) {
    var name = status;
    switch (status) {
      case "10":
        name = "待发货";
        break;

      case "11":
        name = "待付款";
        break;

      case "20":
        name = "已付款";
        break;

      case "30":
        name = "已发货";
        break;

      case "40":
        name = "完成";
        break;

      case "0":
        name = "取消";
        break;

      default:
        name = status;
        break;
    }

    return name;
  }

  async getProducts(customerId, keyword, page) {
    const user = JSON.parse( localStorage.getItem('user') );
    const storeId = user.store_id;
    const result = await this.http.get('/goods?store_id=' + storeId + '&page=' + page 
      + '&user_id=' + customerId
      + '&keyword=' + keyword);

    return result;
  }

  async remove(id) {
    return await this.http.delete('/order/delete?id=' + id);
  }

  async cancel(id) {
    return await this.http.put('/order/cancel?id=' + id);
  }

  async confirm(id, amount) {
    return await this.http.put('/order/delivery-confirm?id=' + id + '&payed_amount=' + amount);
  }

  async submit(formData) {
    return await this.http.post('/order/wx-create', qs.stringify(formData), {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      }
    });
  }

  async getOrder(id) {
    return await this.http.get('/order/view?balance_flag=0&id=' + id);
  }

  async getProduct(id, userId) {
    return await this.http.get('/goods/view?id=' + id + '&user_id=' + userId);
  }

  async getCustomers(keyword, page) {
    return await this.http.get('/delivery-link/my-guest?type=0&keyword=' + keyword + '&page=' + page);
  }
}

export default Order

